package com.twenty.seven.toolbox.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author 001
 * @Date 2020/1/8 9:25
 * @TODO 主要是添加序列化和反序列化
 **/
@Component
public class JSRedisTemplate<E> {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    SerializeUtil<E> serializeUtil;

    /**
     * 根据key获取
     * @param key
     * @return
     */
    public E get(String key){
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        return serializeUtil.unserialize(operations.get(key));
    }

    /**
     * 设置永久性key-value
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key,E value){
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        try{
            operations.set(key,serializeUtil.serialize(value));
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 设置key-value，time保存时间，单位：秒
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean set(String key, E value, long time){
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        try{
            operations.set(key,serializeUtil.serialize(value), time, TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除
     * @param key
     * @return
     */
    public boolean delete(String key) {
        try {
            redisTemplate.delete(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 清除
     */
    public void clear() {
        Set<String> keySet = redisTemplate.keys("*");
        redisTemplate.delete(keySet);
    }

    /**
     * 大小
     * @return
     */
    public int size() {
        Set<String> keySet = redisTemplate.keys("*");
        return keySet.size();
    }

    /**
     * 所有key
     * @return
     */
    public Set<String> keySet() {
        return redisTemplate.keys("*");
    }

    /**
     * 所有值得集合
     * @return
     */
    public Collection<E> values() {
        Set<String> keySet = redisTemplate.keys("*");
        List<E> values = new ArrayList<E>();
        Iterator<String> iterator = keySet.iterator();
        while ( iterator.hasNext() ) {
            values.add(get(iterator.next()));
        }
        return values;
    }

    /**
     * 获取匹配的key
     * @param pattern
     * @return
     */
    public Set<String> keys(String pattern){
        return redisTemplate.keys(pattern);
    }

    /**
     * 删除匹配的值
     * @param keys
     * @return
     */
    public Long delete(Collection<String> keys){
        return redisTemplate.delete(keys);
    }
}
